﻿using Newtonsoft.Json;

namespace DingTalk.Corp.Message.Models
{
    /// <summary>
    /// 消息结构
    /// </summary>
    [JsonObject]
    public class MessageRequest
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="agentId"></param>
        /// <param name="message"></param>
        public MessageRequest(long agentId, DingTalkMessage message)
        {
            AgentID = agentId;
            Message = message;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("userid_list")]
        public string UserList
        {
            get
            {
                if (UserIDs == null || UserIDs.Length <= 0)
                    return null;
                return string.Join(",", UserIDs);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string[] UserIDs { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("to_all_user")]
        public bool ToAllUser { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("dept_id_list")]
        public string DeptList
        {
            get
            {
                if (DeptIDs == null || DeptIDs.Length <= 0)
                    return null;
                return string.Join(",", DeptIDs);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string[] DeptIDs { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("agent_id")]
        public long AgentID { get; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msg")]
        public DingTalkMessage Message { get; }

    }
    /// <summary>
    /// 
    /// </summary>
    [JsonObject]
    public abstract class DingTalkMessage
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public abstract string MessageType
        {
            get;
        }
    }
    /// <summary>
    /// {
    ///        "msg":{
    ///	        "msgtype":"text",
    ///	        "text":{
    ///		        "content":"小小的测试111"
    ///	        }
    ///        },
    ///        "to_all_user":false,
    ///        "agent_id":1900880945,
    ///        "userid_list":"14094130849562"
    ///    }
    /// </summary>
    [JsonObject]
    public class TextMessage : DingTalkMessage
    {
        string content;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public TextMessage(string content)
        {
            this.content = content;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "text"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("text")]
        public object Text
        {
            get
            {
                return new { content };
            }
        }
    }
    /// <summary>
    /// {
	///     "msg":{
	///      "link":{
	///       "picUrl":"http://dev.platform.annow.cn/file/load/sys?fileId=20220329160705553884E5D80F4845A87FB4BA9363926F",
	///       "messageUrl":"http://dev.ui.annow.cn/billsystem/#/Payment/approval/20220926021458818AE7F169474E84995617259721850A/20220926101342-704659",
	///       "text":"这是一条审批信息",
	///       "title":"图文混测"
	///      },
	///      "msgtype":"link",
	///      "text":{
    ///     "content":"小小的测试111"
    ///     
    ///         }
	///     },
	///     "to_all_user":false,
	///     "agent_id":1900880945,
	///     "userid_list":"14094130849562"
    /// }
    /// </summary>
    [JsonObject]
    public class LinkMessage: DingTalkMessage
    {
        string title, text, messageUrl, picUrl;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        /// <param name="messageUri"></param>
        /// <param name="pictureUri"></param>
        public LinkMessage(string title, string text, string messageUri, string pictureUri)
        {
            this.title = title;
            this.text = text;
            this.messageUrl = messageUri;
            this.picUrl = pictureUri;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType
        {
            get { return "link"; }
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("link")]
        public object Link
        {
            get
            {
                return new { this.title, this.text, this.messageUrl, this.picUrl };
            }
        }
    }
    /// <summary>
    /// {
    ///     "msgtype": "oa",
    ///      "oa": {
    ///         "message_url": "http://dingtalk.com",
    ///         "head": {
    ///             "bgcolor": "FFBBBBBB",
    ///             "text": "头部标题"
    ///         },
    ///         "body": {
    ///             "title": "正文标题",
    ///             "form": [
    ///                 {
    ///                     "key": "姓名:",
    ///                     "value": "张三"
    ///                 },
    ///                 {
    ///                     "key": "年龄:",
    ///                     "value": "20"
    ///                 },
    ///                 {
    ///                     "key": "身高:",
    ///                     "value": "1.8米"
    ///                 },
    ///                 {
    ///                     "key": "体重:",
    ///                     "value": "130斤"
    ///                 },
    ///                 {
    ///                     "key": "学历:",
    ///                     "value": "本科"
    ///                 },
    ///                 {
    ///                     "key": "爱好:",
    ///                     "value": "打球、听音乐"
    ///                 }
    ///             ],
    ///             "rich": {
    ///                 "num": "15.6",
    ///                 "unit": "元"
    ///             },
    ///             "content": "大段文本大段文本大段文本大段文本大段文本大段文本",
    ///             "image": "@lADOADmaWMzazQKA",
    ///             "file_count": "3",
    ///             "author": "李四 "
    ///         }
    ///     },
    ///     "to_all_user":false,
    ///     "agent_id":1900880945,
    ///     "userid_list":"14094130849562"
    /// }
    /// </summary>
    [JsonObject]
    public class OAMessage : DingTalkMessage
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("msgtype")]
        public override string MessageType => "oa";

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("oa")]
        public OAData OA
        {
            get;
            set;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class OAData
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("message_url")]
        public string MessageUrl { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("head")]
        public OAHeader Header
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("body")]
        public OABody Body
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("status_bar")]
        public OAStatusBar StatusBar
        {
            get;
            set;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    [JsonObject]
    public class OAHeader
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("bgcolor")]
        public string BgColor { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("text")]
        public string Text { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    [JsonObject]
    public class OABody
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("title")]
        public string Title
        {
            get; set;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("content")]
        public string Content
        {
            get;set;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("image")]
        public string Image
        {
            get;set;
        }
        /// <summary>
        /// 
        /// </summary>
        public IDictionary<string, string> FormData
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("form")]
        public object Form
        {
            get
            {
                if (this.FormData == null)
                    return null;
                return this.FormData.ToArray();
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [JsonObject]
    public class OAStatusBar
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("status_value")]
        public string StatusValue { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("status_bg")]
        public string StatusBgColor { get; set; }
    }
}
